package jfox.platform.datadict.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jfox.platform.datadict.entity.DictCategory;
import jfox.platform.datadict.entity.DictItem;

/**
 * 建立字典数据的缓存
 *
 * 由 SuperAction 负责初始化
 *
 * @author <a href="mailto:yang_y@sysnet.com.cn">Young Yang</a>
 */
public class DataDicts {

    private Map<String, DictCategory> categoryMap = new HashMap<String, DictCategory>();

    private Map<String, List<DictItem>> itemMap = new HashMap<String, List<DictItem>>();

    private long dictCategoriesHash = 0;
    private long dictItemsHash = 0;

    private static final DataDicts instance = new DataDicts();

    private DataDicts() {
    }

    public static DataDicts getInstance(){
        return instance;
    }

    public static DataDicts initDataDictHelper(List<DictCategory> dictCategories, List<DictItem> dictItems){

        if(dictCategories.hashCode() != instance.dictCategoriesHash) {
            instance.dictCategoriesHash = dictCategories.hashCode();
            instance.categoryMap.clear();
            for(DictCategory dictCategory : dictCategories){
                instance.categoryMap.put(dictCategory.getCode(), dictCategory);
            }
        }

        if(dictItems.hashCode() != instance.dictItemsHash){
            instance.dictItemsHash = dictItems.hashCode();
            instance.itemMap.clear();
            for(DictItem dictItem : dictItems){
                String categoryCode = dictItem.getCategoryCode();
                if(!instance.itemMap.containsKey(categoryCode)){
                    List<DictItem> items = new ArrayList<DictItem>();
                    items.add(dictItem);
                    instance.itemMap.put(categoryCode, items);
                }
                else {
                    List<DictItem> items = instance.itemMap.get(categoryCode);
                    items.add(dictItem);
                }
            }

        }
        return instance;
    }

    public DictCategory getDictCategory(String categoryCode){
        return categoryMap.get(categoryCode);
    }

    public List<DictItem> getDictItemsByCategory(String categoryCode){
        return itemMap.get(categoryCode);
    }

    public DictItem getDictItem(String categoryCode, String itemCode){
        if(!itemMap.containsKey(categoryCode)){
            return null;
        }
        List<DictItem> dictItems = itemMap.get(categoryCode);
        for(DictItem item : dictItems){
            if(item.getStringValue().equals(itemCode)){
                return item;
            }
        }
        return null;
    }

    public DictItem getDictItem(String categoryCode, int itemCode){
        if(!itemMap.containsKey(categoryCode)){
            return null;
        }
        List<DictItem> dictItems = itemMap.get(categoryCode);
        for(DictItem item : dictItems){
            if(item.getNumberValue() == itemCode){
                return item;
            }
        }
        return null;
    }

    public static void main(String[] args) {

    }
}
